package com.cnzha.mr.inputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @Author: zhaoxin
 * @email: zdkk@foxmail.com
 * @Date: 2020/4/2 21:05
 */
public class WholeRecordReader extends RecordReader<Text, BytesWritable> {

    FileSplit fileSplit = null;
    Configuration configuration = null;
    FileSystem fileSystem = null;
    BytesWritable value = new BytesWritable();
    Text key = new Text();
    private boolean isProgress = true;


    @Override
    public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {

        fileSplit = (FileSplit) split;
        configuration = context.getConfiguration();
    }

    @Override
    public boolean nextKeyValue() throws IOException, InterruptedException {
        if (isProgress) {
            //缓冲区
            byte[] bytes = new byte[(int) fileSplit.getLength()];

            FileSystem fileSystem = null;
            FSDataInputStream fsd = null;
            try {
                Path path = fileSplit.getPath();
                fileSystem = path.getFileSystem(configuration);
                fsd = fileSystem.open(path);
                IOUtils.readFully(fsd, bytes, 0, bytes.length);
                value.set(bytes, 0, bytes.length);
                //key
                String keyStr = fileSplit.getPath().toString();
                key.set(keyStr);
            } finally {
                IOUtils.closeStream(fileSystem);
                IOUtils.closeStream(fsd);
            }
            isProgress = false;
            return true;
        }
        return false;
    }

    @Override
    public Text getCurrentKey() throws IOException, InterruptedException {
        return key;
    }

    @Override
    public BytesWritable getCurrentValue() throws IOException, InterruptedException {
        return value;
    }

    @Override
    public float getProgress() throws IOException, InterruptedException {
        return 0;
    }

    @Override
    public void close() throws IOException {

    }
}
